package com.zdxlz.desensitize.domain;

import com.zdxlz.desensitize.constant.DesensitizeType;

import java.util.HashMap;
import java.util.Map;

/**
 * JSON脱敏规则结构树
 */
public class RuleTree {
    /**
     * 当前节点
     */
    private String id;

    /**
     * 子节点
     */
    private Map<String, RuleTree> children;

    /**
     * 脱敏类型
     */
    private DesensitizeType type;

    /**
     * 脱敏类型的值
     */
    private Integer value;

    /**
     * 是否是叶子节点
     */
    private boolean isLeaf = true;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Map<String, RuleTree> getChildren() {
        return children;
    }

    public RuleTree getChild(String childId) {
        if (isLeaf || !children.containsKey(childId)) {
            return null;
        }
        return children.get(childId);
    }

    public void addChildren(RuleTree ruleTree) {
        if (isLeaf) {
            this.children = new HashMap<>();
        }
        this.children.put(ruleTree.getId(), ruleTree);
        isLeaf = false;
    }

    public boolean hasChild(String childId) {
        if (isLeaf) {
            return false;
        }
        return children.containsKey(childId);
    }

    public DesensitizeType getType() {
        return type;
    }

    public Integer getValue() {
        return value;
    }

    public void setValue(Integer value) {
        this.value = value;
    }

    public void setType(DesensitizeType type) {
        this.type = type;
    }

    public boolean isLeaf() {
        return isLeaf;
    }
}
